﻿/*********************************************
 创建者：  george
 创建时间：2016-10-24
 功能说明：系统权限(模块、按钮)
 ********************************************/
using com.nm.ApiServer;
using com.nm.ApiServer.Attributes;
using com.nm.Attributes;
using System.ComponentModel;
using george.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using george.Helper;
using com.nm.models;
using com.nm;
using george.Web.Helper;
using com.nm.db.Models.Systems;
using System.Web.Caching;
using george.Web;
using BaseAPI.model;
using Nancy.Extensions;
using com.nm.library;

namespace BaseAPI
{
   [Description("系统权限(模块、按钮)")]
    public  partial class RightHandler : BaseHandler
    {
        [MustLogin]
        [Description("根据用户角色加载模块")]
        [ReturnType(typeof(IList<PageModuleList>))]
        public ApiResult GetModule()
        {
            var dc = new SysEntities();
            var cacheKey = "all-module-list-" + this.UserId;
            var allModuleList =  myCache.Get<IList<PageModuleList>>(cacheKey);
            if (allModuleList == null || !myCache.Exists(cacheKey))
            {
                var query = GetModuleList(1, dc);
                allModuleList = query.Select(t => new PageModuleList
                {
                    Grade = (int) t.Grade,
                    ID = (int) t.ID,
                    ModuleCode = t.ModuleCode,
                    ModuleName = t.ModuleName,
                    Url = t.Url,
                    Sort = (int) t.Sort,
                    SuperiorModule = (int) t.SuperiorModule,
                    Type = (int) t.Type,
                    Childrens = GetChildren((int) t.ID)
                }).ToList();
                myCache.Set(cacheKey, allModuleList);
            }
            return Success(allModuleList);
        }

        /// <summary>
        /// 获取一级菜单
        /// </summary>
        /// <param name="moduleId">菜单编号</param>
        /// <param name="dc"></param>
        /// <returns></returns>
        IList<PageModuleList> GetModuleList(int? moduleId, SysEntities dc)
        {
            var userid = Convert.ToInt32(UserInfo.UserID);
            var query = (from c in dc.sys_v_module
                        where c.SuperiorModule == moduleId && c.UserID== userid
                        && c.IsEffective && c.IsRemove==false
                        select new PageModuleList
                        {
                            ID = c.ID,
                            Grade = c.Grade,
                            ModuleCode = c.ModuleCode,
                            ModuleName = c.ModuleName,
                            Sort = (int)c.Sort,
                            SuperiorModule = c.SuperiorModule,
                            Type = c.Type,
                            Url = c.Url
                        }).ToList();
            if (UserInfo.RoleType)
                query = (from c in dc.sys_module
                         where c.SuperiorModule == moduleId && c.IsEffective && c.IsRemove == false
                         group c by new { c.Grade, c.ID, c.IsEffective, c.IsRemove, c.Memo, c.ModuleCode, c.ModuleName, c.Sort, c.SuperiorModule, c.Type, c.Url } into g
                         select new PageModuleList()
                         {
                             ID = g.Key.ID,
                             Grade = g.Key.Grade,
                             ModuleCode = g.Key.ModuleCode,
                             ModuleName = g.Key.ModuleName,
                             Sort = (int)g.Key.Sort,
                             SuperiorModule = g.Key.SuperiorModule,
                             Type = g.Key.Type,
                             Url = g.Key.Url
                         }).ToList();
            return query;
            //return query.ToList().Concat(query.ToList().SelectMany(t => GetModuleList(t.ID, dc)));
        }

        /// <summary>
        /// 获取子菜单
        /// </summary>
        /// <param name="parnetID"></param>
        /// <returns></returns>
        public List<PageModuleList> GetChildren( int parnetID)
        {
            var dc = new SysEntities();
            var query = (from c in dc.sys_v_module
                         where c.SuperiorModule == parnetID && c.UserID == this.UserId && c.IsEffective && c.IsRemove == false
                         select c).ToList().Select(c => new PageModuleList
                         {

                             ID = c.ID,
                             Grade = c.Grade,
                             ModuleCode = c.ModuleCode,
                             ModuleName = c.ModuleName,
                             Sort = (int)c.Sort,
                             SuperiorModule = c.SuperiorModule,
                             Type = c.Type,
                             Url = c.Url,
                             Childrens = GetChildren(c.ID)
                         }).ToList();
            if (UserInfo.RoleType)
                query = (from c in dc.sys_module
                         where c.SuperiorModule == parnetID && c.IsEffective && c.IsRemove == false
                         group c by new { c.Grade, c.ID, c.IsEffective, c.IsRemove, c.Memo, c.ModuleCode, c.ModuleName, c.Sort, c.SuperiorModule, c.Type, c.Url } into g
                         select g.Key).ToList().Select(t => new PageModuleList()
                         {
                             ID = t.ID,
                             Grade = t.Grade,
                             ModuleCode = t.ModuleCode,
                             ModuleName = t.ModuleName,
                             Sort = (int)t.Sort,
                             SuperiorModule = t.SuperiorModule,
                             Type = t.Type,
                             Url = t.Url,
                             Childrens = GetChildren(t.ID)
                         }).ToList();
            return query;
        }

        [MustLogin]
        [Description("根据用户角色加载模块按钮")]
        [ReturnType(typeof(IList<ModifyBtnData>))]
        public ApiResult GetBtnRight([ParamDescription("模块ID")]int ModuleID)
        {
            var dc = new SysEntities();
            var cacheKey = "all-btn-list-" + this.UserId;
            var allbtnList = myCache.Get<IList<ModifyBtnData>>(cacheKey);
            if (allbtnList !=null && !allbtnList.Any() || !myCache.Exists(cacheKey))
            {
                var query = (from b in dc.sys_button
                    join p in dc.sys_module_privilege on b.ID equals p.BtnID
                    join r in dc.sys_user_role on p.RoleID equals r.RoleID
                    where b.IsEffective && b.IsRemove == false && p.ModuleID == ModuleID && r.UserID == UserId
                             select new
                    {
                        b
                    });
                if (UserInfo.RoleType)
                    query = (from mb in dc.sys_module_button
                             join b in dc.sys_button on mb.ButtonID equals b.ID
                             where mb.ModuleID == ModuleID && b.IsEffective && b.IsRemove == false
                             select new { b });
                allbtnList = query.ToList().OrderBy(t => t.b.Sort).Select(c => new ModifyBtnData
                {
                    BtnEvent = c.b.BtnEvent,
                    CssID = c.b.CssID,
                    BtnID = c.b.BtnID,
                    BtnName = c.b.BtnName,
                    BtnType = c.b.BtnType,
                    ID = c.b.ID,
                    IsEffective = c.b.IsEffective,
                    Memo = c.b.Memo,
                    MoldID = c.b.MoldID,
                    Sort = c.b.Sort,
                    BackColor = c.b.BackColor,
                    Icon = c.b.ICon
                }).DistinctBy(r => r.BtnID).ToList();
                myCache.Set(cacheKey, allbtnList);
            }
            return Success(allbtnList);
        }
    }
}
